#!/usr/bin/env python
""" AnyBackup is a windows specific backup program to keep multiple volumes in sync with multiple volumes
    Copyright (C) 2011  Andrew Hopkinson

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Exported Classes:

    messagingMixin -- a small mixin to support setting and writing to a messaging queue
    pathMixin -- provides a path building function, for reasons unknown os.path.join sometimes returns nonsensical results
    """

import os
from contextlib import contextmanager 

class messagingMixin:
        def setMessagingQueue(self,queue):
                global messagingQueue
                messagingQueue = queue
                
        def sendMessage(self,message):
                if messagingQueue:
                        messagingQueue.put(message,1)
                else:
                        print message
                        
class pathMixin:
        def toFullPath(self,path):
                path = path.replace('\\\\','\\')
                return u'\\\\?\\'+path
        
        def fullPath(self,*args):
                return u'\\\\?\\'+self.createPath(*args)
        
        def createPath(self,*args):
                pathStr = u''
                for piece in args:
                        if pathStr:
                                pathStr += os.sep
                        pathStr += piece
                while '\\\\' in pathStr:
                        pathStr = pathStr.replace('\\\\','\\')
                return pathStr

        @contextmanager 
        def working_directory(self,directory): 
                """ 
                This function is intended to be used as a ``with`` statement context 
                manager. It allows you to replace code like this: 
                .. python:: 
         
                original_directory = _os.getcwd() 
                try: 
                        _os.chdir(some_dir) 
                        ... bunch of code ... 
                finally: 
                        _os.chdir(original_directory) 
           
              with something simpler: 
           
              .. python :: 
           
                  from __future__ import with_statement 
                  from grizzled.os import working_directory 
           
                  with working_directory(some_dir): 
                      ... bunch of code ... 
           
              :Parameters: 
                  directory : str 
                      directory in which to execute 
           
              :return: yields the ``directory`` parameter 
              """ 
                original_directory = os.getcwd() 
                try: 
                        os.chdir(directory) 
                        yield directory    
                finally: 
                        os.chdir(original_directory) 
                
                